package ua.com.kangaroo.search;

import java.util.ArrayList;
import java.util.Arrays;

import ua.com.kangaroo.basic.SchoolsData;

public class SchoolSearch {
	private ArrayList<Integer> searchResult = new ArrayList<Integer>();
	
	/**
	 * @param keyword String which method should search in fullnames of schools
	 * @return true if there is at least one such school or false in the other way
	 */
	public boolean search(String keyword) {
		return search(0, keyword);			
	}
	
	/**
	 * @param region number of region in which keyword should be search
	 * @param keyword String which method should search in fullnames of schools
	 * @return true if there is at least one such school or false in the other way
	 */
	public boolean search(int region, String keyword) {
		String newKeyword = keyword.toLowerCase();
		boolean result = false; 
		if ((0 <= region) || (region < 26)) {
			if (region == 0) {
				int idOfFirstSchool = 999;
				idOfFirstSchool = SchoolsData.getNextSchool(idOfFirstSchool);
				int i = SchoolsData.getNextSchool(idOfFirstSchool);
				String iFullName = SchoolsData.getSchoolById(i).getFullName().toLowerCase();
				if (iFullName.contains(newKeyword)) {
					searchResult.add(i);
					result = true;
				}
				i = SchoolsData.getNextSchool(i);
				while (i != idOfFirstSchool) {
					iFullName = SchoolsData.getSchoolById(i).getFullName().toLowerCase();
					if (iFullName.contains(newKeyword)) {
						searchResult.add(i);
						result = true;
					}
					i = SchoolsData.getNextSchool(i);
			    }  
			} else {
				if (SchoolsData.firstIdInRegion(region) < SchoolsData.lastIdInRegion(region)) {
					int i = SchoolsData.firstIdInRegion(region);
					String iFullName = SchoolsData.getSchoolById(i).getFullName().toLowerCase();
					if (iFullName.contains(newKeyword)) {
						searchResult.add(i);
						result = true;
					}
					i = SchoolsData.getNextSchool(i);
					while (i != SchoolsData.firstIdInRegion(region)) {
						iFullName = SchoolsData.getSchoolById(i).getFullName().toLowerCase();
						if (iFullName.contains(newKeyword)) {
							searchResult.add(i);
							result = true;
						}
						i = SchoolsData.getNextSchool(i);
				    }	
				} else result = false;
			}
			return result;
		} else return false;
	}
	
	/**
	 * @param region numbers of regions in which keyword should be search
	 * @param keyword String which method should search in fullnames of schools
	 * @return true if there is at least one such school or false in the other way
	 */
	public boolean search(int[] region, String keyword) {
		String newKeyword = keyword.toLowerCase();
		Arrays.sort(region);
		if (!search(region[0], newKeyword)) return false;
		for (int i = 1; i < region.length; i++) {
			if (region[i] != region[i - 1]) {
				if (!search(region[i], newKeyword)) return false;
			}
		}
		return true;
	}
	
	/**
	 * @return search result
	 */
	public int[] result(){
		int[] result = new int[searchResult.size()];
		for (int i = 0; i < searchResult.size(); i++) {
			result[i] = searchResult.get(i);
		}
		return result;
	}
}
